package com.api.user.controller;

import com.api.core.annotation.PowerEnable;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.user.model.CommodityAdd;
import com.api.user.model.CouponUse;
import com.api.user.service.CommodityOrderService;
import com.api.user.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;



/**
* Created by wanghuiwen on 2021/04/12.
*/
@PowerEnable(name = "商品",url = "/commodity")
@Api(value = "商品", tags = {"商品"})
@RestController
@RequestMapping("/commodity")
public class CommodityController extends Ctrl{
    @Resource
    private CommodityService commodityService;

    @Resource
    private CommodityOrderService commodityOrderService;

    @ApiOperation(value = "商品添加/修改", tags = {"商品"}, notes = "商品添加/修改")
    @PostMapping(value="/add",name="商品添加/修改")
    public Result add(@RequestBody CommodityAdd commodityAdd) {
        return commodityService.add(commodityAdd);
    }


    @ApiOperation(value = "用户端-购买商品", tags = {"商品"}, notes = "用户端-购买商品")
    @PostMapping(value="/order/buy",name="用户端-购买商品")
    public Result orderBuy(@RequestBody CouponUse couponUse, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        return commodityService.orderBuy(couponUse,userId);
    }

    @ApiOperation(value = "商品下架/删除", tags = {"商品"}, notes = "商品下架/删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "commodityId",required=true, value = "商品id", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "state",required=true, value = "状态", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/state/update",name="商品下架/删除")
    public Result stateUpdate(Long commodityId,Integer state) {

        return commodityService.stateUpdate(commodityId,state);
    }


    @ApiOperation(value = "管理端-订单发货", tags = {"商品"}, notes = "管理端-订单发货")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderId",required=true, value = "订单id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/order/send",name="管理端-订单发货")
    public Result orderSend(@RequestParam Long orderId, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        return commodityService.orderSend(orderId,userId);
    }



    @ApiOperation(value = "商品列表信息", tags = {"商品"}, notes = "商品列表信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "commodityName", value = "商品名称", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "categoryId", value = "商品分类", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "commodityState", value = "商品状态", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "商品列表信息")
    public Result list(
                       @RequestParam(defaultValue = "") String commodityName,
                       @RequestParam(defaultValue = "") Long categoryId,
                       @RequestParam(defaultValue = "") Long commodityState,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {

        Map<String,Object> params = new HashMap<>();
        params.put("commodityName",commodityName);
        params.put("categoryId",categoryId);
        params.put("commodityState",commodityState);
        return commodityService.list(params, order, page, size);
    }
    @PostMapping(value = "/user/list", name = "商品列表信息")
    public Result userList(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String  commodityName,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Map<String,Object> params = new HashMap<>();
        params.put("categoryId",categoryId);
        params.put("commodityName",commodityName);
        return commodityService.userList(params, order, page, size);
    }

    @PostMapping(value = "/detail", name = "商品列表信息")
    public Result detail(Long commodityId) {
        return commodityService.detail(commodityId);
    }

    @ApiOperation(value = "用户端-订单列表", tags = {"商品"}, notes = "用户端-订单列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderState", value = "订单状态", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/order/user/list", name = "用户端-订单列表")
    public Result orderUserList(
                        @RequestParam(required = false) Integer orderState,
                        @RequestParam(required = false) Integer orderId,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                        Authentication authentication) {

        Map<String,Object> params = new HashMap<>();
        params.put("orderState",orderState);
        params.put("orderId",orderId);
        params.put("userId",getAuthUser(authentication).getId());
        return commodityOrderService.orderUserList(params, order, page, size);
    }


    @ApiOperation(value = "管理端-订单列表", tags = {"商品"}, notes = "管理端-订单列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "commodityName", value = "商品名称", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "orderState", value = "订单状态", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "userId", value = "下单用户", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/order/list", name = "用户端-订单列表")
    public Result orderList(
                       @RequestParam(defaultValue = "") Long orderId,
                       @RequestParam(defaultValue = "") String commodityName,
                       @RequestParam(defaultValue = "") Long orderState,
                       @RequestParam(defaultValue = "") Long userId,
                       @RequestParam(defaultValue = "") Long startTime,
                       @RequestParam(defaultValue = "") Long endTime,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {

        Map<String,Object> params = new HashMap<>();
        params.put("orderId",orderId);
        params.put("commodityName",commodityName);
        params.put("orderState",orderState);
        params.put("userId",userId);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        return commodityOrderService.orderUserList(params, order, page, size);
    }
    @PostMapping(value = "/order/completed", name = "用户端-完成订单")
    public Result completed(@RequestParam Long orderId) {

        return commodityOrderService.completed(orderId);
    }
}
